package com.mall.controller.backend;

import com.mall.common.Const;
import com.mall.common.ServerResponse;
import com.mall.pojo.User;
import com.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by xiaoyao on 2017/8/21.
 */
@Controller
@RequestMapping("/manage/user/")
public class UserManagerController {
    @Autowired
    private IUserService iUserService;

    /**
     * 管理员登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(User user, HttpSession session){
        ServerResponse<User> response = iUserService.login(user);
        if(response.isSuccess()){
            User userCheck = response.getData();
            if(userCheck.getRole() == Const.Role.ROLE_ADMIN){
                session.setAttribute(Const.CURRENT_USER,userCheck);
                Const.sessionId  = session.getId();
                return response;
            }else{
                return ServerResponse.createByErrorMessage("不是管理员登录");
            }
        }
        return response;
    }
}
